﻿// Author:  Marco van Wieren <mailto:mvwieren@gmail.com>
// Year:    2012
// License: Microsoft Public License (Ms-PL)

using System;
using System.Text;
using Microsoft.SharePoint;

namespace Mvw.SharePoint.Taxonomy.AutoTagging.Licenses
{
    public static class TrialLicense
    {
        public static bool verifyTrialStartDate(SPWeb rootWeb)
        {
            return true;
            /*
            if (rootWeb.AllProperties["backup_81CA1F59805E"] != null)
            {
                string startDateAsString = obfuscateTrialStartDate(rootWeb.AllProperties["backup_81CA1F59805E"].ToString());
                try
                {
                    if (DateTime.Today.Subtract(DateTime.Parse(startDateAsString)).Days < 15)
                    {
                        return true;
                    }
                }
                catch { }
            }
            else
            {
                // Only true for the first time
                rootWeb.AllProperties["backup_81CA1F59805E"] = obfuscateTrialStartDate(DateTime.Today.ToString());
                rootWeb.Update();
                return true;
            }

            // Default return
            return false;
            */
        }

        private static string obfuscateTrialStartDate(string textToObfuscate)
        {
            int key = 129;
            StringBuilder stringIn = new StringBuilder(textToObfuscate);
            StringBuilder stringOut = new StringBuilder(textToObfuscate.Length);
            char c;
            for (int i = 0; i < textToObfuscate.Length; i++)
            {
                c = stringIn[i];
                c = (char)(c ^ key);
                stringOut.Append(c);
            }
            return stringOut.ToString();
        }
    }
}
